import 'dart:typed_data';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:pag/pag.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHome(),
    );
  }
}

class MyHome extends StatefulWidget {
  @override
  _MyHomeState createState() => _MyHomeState();
}

class _MyHomeState extends State<MyHome> {
  GlobalKey<PAGViewState> _fansDanceKey =
      GlobalKey<PAGViewState>(debugLabel: _assetFans);
  GlobalKey<PAGViewState> _assetDanceKey =
      GlobalKey<PAGViewState>(debugLabel: _assetFans);
  GlobalKey<PAGViewState> get assetPagKey =>
      _pagAsset == _assetFans ? _fansDanceKey : _assetDanceKey;

  final GlobalKey<PAGViewState> networkPagKey = GlobalKey<PAGViewState>();
  final GlobalKey<PAGViewState> bytesPagKey = GlobalKey<PAGViewState>();

  Uint8List? bytesData;

  // 本地加载资源
  static const String _assetFans = 'assets/fans.pag';
  static const String _assetDance = 'assets/dance.pag';
  static const String _assetError = 'assets/error.pag';
  String _pagAsset = _assetFans;

  void changeAsset() {
    setState(() {
      _pagAsset = _pagAsset == _assetFans ? _assetDance : _assetFans;
    });
  }

  @override
  void initState() {
    rootBundle.load("assets/fans.pag").then((data) {
      // Uint8List dataa = Uint8List.view(data.buffer);
      // print(dataa?.isNotEmpty);
      setState(() {
        bytesData = Uint8List.view(data.buffer);
        // print(bytesData);
      });
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text('PAGView example a55555pp'),
        ),
        body: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Image.network('http://7.247.80.237:3000/186.png'),
              // Container(
              //   width: 200,
              //   height: 200,
              //   color: Color.fromARGB(255, 255, 0, 1),
              //   child: PAGView.network(
              //     "http://7.247.80.237:3000/cat.pag",
              //     repeatCount: PAGView.REPEAT_COUNT_LOOP,
              //     initProgress: 1,
              //     width: 100,
              //     height: 100,
              //     autoPlay: true,
              //     key: networkPagKey,
              //   ),
              // ),
              Container(
                width: 200,
                height: 200,
                color: Color.fromARGB(255, 255, 0, 1),
                child: Visibility(
                    visible: bytesData?.isNotEmpty == true,
                    child: PAGView.bytes(
                      bytesData,
                      repeatCount: PAGView.REPEAT_COUNT_LOOP,
                      initProgress: 0.25,
                      width: 400,
                      height: 400,
                      autoPlay: true,
                      key: bytesPagKey,
                    )),
              ),
            ],
          ),
        ));
  }
}
